package com.lovestudying.controller;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;
import com.lovestudying.entity.Video;
import com.lovestudying.service.VideoService;
import com.lovestudying.service.impl.VideoServiceImpl;
import com.lovestudying.util.PageInfo;

/**
 * Servlet implementation class VideoServlet
 */
@WebServlet("/VideoServlet")
public class VideoServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	VideoService videoService = new VideoServiceImpl();

	Gson gson = new Gson();

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public VideoServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String op = request.getParameter("op");
		switch (op) {
		case "getVideoInfo":
			getVideoInfo(request, response);
			break;
		case "getVideosByCourseId":
			getVideosByCourseId(request, response);
			break;
		default:
			break;
		}

	}

	/**
	 * 根据课程id获取所有视频
	 * 
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	private void getVideosByCourseId(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String courseId = request.getParameter("courseId");
		String orderBy = request.getParameter("orderBy");
		Integer pageNum = Integer.parseInt(request.getParameter("pageNum"));
		Integer pageSize = Integer.parseInt(request.getParameter("pageSize"));
		String desc = request.getParameter("desc");
		PageInfo<Video> videos = videoService.getVideosByCourseId(courseId, orderBy, desc, pageSize, pageNum);
		String Jvideo = gson.toJson(videos);
		response.getWriter().print(Jvideo);

	}

	private void getVideoInfo(HttpServletRequest request, HttpServletResponse response) {

	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
